package com.exam.web.action.uploadfile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.exam.bean.PageView;
import com.exam.bean.QueryResult;
import com.exam.bean.uploadfile.UploadFile;
import com.exam.service.uploadfile.UploadFileService;
import com.exam.utils.ValidateFile;
import com.exam.web.action.privilege.Privilege;

@Controller("uploadFileAction")
@Scope("prototype")
public class UploadFileAction extends ActionSupport {
	
	private static final long serialVersionUID = -9056696759526525791L;

	@Resource
	private UploadFileService uploadFileService;
	

	private int page;//分页
	private PageView<UploadFile> pageView;//记录集
	
	private Integer[] fileids;
	
	private File imagefile;//上传文件
	private String imagefileContentType;//上传文件类型
	private String imagefileFileName;//上传文件名称
	
	
	/**
	 * 列表显示文件
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="uploadfile",privilegeValue="view")
	public String list()throws Exception{
		
		this.pageView=new PageView<UploadFile>(4,this.getPage());
		int firstIndex=(this.getPage()-1)*pageView.getMaxResult();
		
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		
		QueryResult<UploadFile> qr=uploadFileService.getScrollData(UploadFile.class
				,firstIndex,pageView.getMaxResult(),orderby);
		
		pageView.setQueryResult(qr);
		
		return SUCCESS;
	}
	
	/**
	 * 上传文件界面
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="uploadfile",privilegeValue="insert")
	public String uploadUI()throws Exception{
		
		return "upload";
	}
	
	/**
	 * 上传文件
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="uploadfile",privilegeValue="insert")
	public String upload()throws Exception{
		
		if(this.getImagefile()!=null&&this.getImagefile().length()>0){
			if(!ValidateFile.validateFileType(this.getImagefile()
					, this.getImagefileContentType(), this.getImagefileFileName())){
				ActionContext.getContext().put("message", "上传文件格式不正确,只允许上传图片/flash动画/word文件/exe文件/pdf文件/txt文件/xls文件/ppt文件");
				ActionContext.getContext().put("urladdress", "file_uploadUI.html");
				return "message";
			}
			
			String ext = ValidateFile.getExtension(this.getImagefileFileName());
			if(("gif".equals(ext) || "jpg".equals(ext) || "bmp".equals(ext) || "png".equals(ext))
					&& this.getImagefile().length()>204800){
				ActionContext.getContext().put("message", "图片不能大于200K");
				ActionContext.getContext().put("urladdress", "file_uploadUI.html");
				return "message";
			}
			else if(this.getImagefile().length()>1024*2048){
				ActionContext.getContext().put("message", "文件不能大于2M");
				ActionContext.getContext().put("urladdress", "file_uploadUI.html");
				return "message";
			}
			
			SimpleDateFormat dateformat=new SimpleDateFormat("yyyy/MM/dd");
			String logopath="/images/uploadfile/"+dateformat.format(new Date());
			
			String realpath = ServletActionContext.getServletContext().getRealPath(logopath);
			File file = new File(realpath);
			if(!file.exists()) file.mkdirs();
			String fileName=UUID.randomUUID().toString()+"."+ValidateFile.getExtension(this.getImagefileFileName());
			FileUtils.copyFile(this.getImagefile(), new File(file, fileName));
			
			UploadFile uploadFile=new UploadFile(logopath+"/"+fileName);
			
			uploadFileService.save(uploadFile);
			ActionContext.getContext().put("imagepath", uploadFile.getFilePath());
			return "uploadfinish";
		}
		else{
			ActionContext.getContext().put("message", "请上传文件");
			ActionContext.getContext().put("urladdress", "file_uploadUI.html");
			return "message";
		}
	}
	
	/**
	 * 删除文件
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="uploadfile",privilegeValue="delete")
	public String delete()throws Exception{
		
		List<String> files=uploadFileService.getFilepath(this.getFileids());
	
		if(files!=null&&files.size()>0){
			for(String file : files){
				String realpath=ServletActionContext.getServletContext().getRealPath(file);
				File deletefile = new File(realpath);
				if(deletefile.exists()){
					deletefile.delete();
				}
			}
			uploadFileService.delete(UploadFile.class, this.getFileids());
			ActionContext.getContext().put("message", "删除文件成功");
			ActionContext.getContext().put("urladdress", "file_list.html");
		}
		else{
			ActionContext.getContext().put("message", "删除文件失败");
			ActionContext.getContext().put("urladdress", "file_list.html");
		}
		return "message";
	}
	
	
	public int getPage() {
		return page<1?1:page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	
	public PageView<UploadFile> getPageView() {
		return pageView;
	}
	public void setPageView(PageView<UploadFile> pageView) {
		this.pageView = pageView;
	}
	
	public Integer[] getFileids() {
		return fileids;
	}
	public void setFileids(Integer[] fileids) {
		this.fileids = fileids;
	}
	
	public File getImagefile() {
		return imagefile;
	}
	public void setImagefile(File imagefile) {
		this.imagefile = imagefile;
	}
	
	public String getImagefileContentType() {
		return imagefileContentType;
	}
	public void setImagefileContentType(String imagefileContentType) {
		this.imagefileContentType = imagefileContentType;
	}
	
	public String getImagefileFileName() {
		return imagefileFileName;
	}
	public void setImagefileFileName(String imagefileFileName) {
		this.imagefileFileName = imagefileFileName;
	}
}
